<?php

	//JACKED Manager session handling script
	
	//including this script will set session handlers,
	////for database-based sessions and start a new session
	
		
	//session open function
	function _open(){
		include_once('jacked_configVars.php');
		include_once('../jacked_configMySQL.php');
		$old = time() - JACKED_SESSION_EXPIRY;
		$query = "DELETE FROM " . JACKED_SESSIONS_TABLE . " WHERE access < '$old'";
		$result = mysql_query($query, JACKED_DEFAULT_LINK);
		return TRUE;
	}
	
	//session close function
	function _close(){
		return true;
	}
	
	//session read function
	function _read($id){
		$answer = '';
		$id = mysql_real_escape_string($id, JACKED_DEFAULT_LINK);
		$query = "SELECT data FROM " . JACKED_SESSIONS_TABLE . " WHERE id = '$id'";
		if($result = mysql_query($query, JACKED_DEFAULT_LINK)){
			if(mysql_num_rows($result)){
				$row = mysql_fetch_assoc($result);
				$answer = $row['data'];
			}
		}
		return $answer;
	}
	
	//session write function
	function _write($id, $data){
		$answer = NULL;
		
		$access = time();
	
		$id = mysql_real_escape_string($id, JACKED_DEFAULT_LINK);
		$access = mysql_real_escape_string($access, JACKED_DEFAULT_LINK);
		$data = mysql_real_escape_string($data, JACKED_DEFAULT_LINK);
		
		$query = "REPLACE INTO " . JACKED_SESSIONS_TABLE . " VALUES ('$id', '$access', '$data')";
		if($result = mysql_query($query, JACKED_DEFAULT_LINK))
			$answer = $result;

		return $answer;
	}
	
	//session destroy function
	function _destroy($id){
		$id = mysql_real_escape_string($id, JACKED_DEFAULT_LINK);
		
		$query = "DELETE FROM " . JACKED_SESSIONS_TABLE . " WHERE id = '$id'";
		$answer = mysql_query($query, JACKED_DEFAULT_LINK);
	
		return $answer;
	}
	
	//session clean function
	function _clean($max){
		$old = time() - $max;
		$old = mysql_real_escape_string($old, JACKED_DEFAULT_LINK);
		
		$query = "DELETE FROM " . JACKED_SESSIONS_TABLE . " WHERE access < '$old'";
		$result = mysql_query($query, JACKED_DEFAULT_LINK);
		
		$answer = $result;
		
		return $answer;
	}
	
	//start the session
	session_set_save_handler('_open', '_close', '_read', '_write', '_destroy', '_clean');

	
	@session_start();
	
	
	/////////////////////////////////
	//JACKED SESSIONS SHENANIGANS    //
	    ///////////////////////////////
	function jackedSession_read($key){
	    $keys = split("\.", $key);
    	$session =& $_SESSION;
    
    	foreach ($keys as $e) {
    		if (!isset($session[$e]) || empty($session[$e]))
    			return false;
    
    		if(is_array($session))
        		$session =& $session[$e];
    	}
	    
	    return $session;
	}
	
	function jackedSession_write($key, $value){
	    $keys = split("\.", $key);
    	$session =& $_SESSION;
    
    	foreach ($keys as $e) {
    		if (!isset($session[$e]) || empty($session[$e]))
    			$session[$e] = array();
    
    		if(is_array($session))
        		$session =& $session[$e];
    	}
	    
	    return $session = $value;
	}	
	
	function jackedSession_delete($key){
	    $keys = split("\.", $key);
    	$session =& $_SESSION;
    
    	foreach ($keys as $e) {
    		if (!isset($session[$e]) || empty($session[$e]))
    			return false;
    
    		if(is_array($session))
        		$session =& $session[$e];
    	}
	    
	    $session = NULL;
	}
	
	function jackedSession_check($key){
    	$keys = split("\.", $key);
    	$session =& $_SESSION;
    
    	foreach ($keys as $e) {
    		if (!isset($session[$e]) || empty($session[$e]))
    			return false;
            
            if(is_array($session))
        		$session =& $session[$e];
    	}
    
    	return true;
	}
	
	function jackedSession_destroy(){
        return session_destroy();
	}
	
	function jackedSession_idExists($sid){
	    $ans = false;
        $id = mysql_real_escape_string($id, JACKED_DEFAULT_LINK);
    	$query = "SELECT access FROM " . JACKED_SESSIONS_TABLE . " WHERE id = '$sid'";
    	if($result = mysql_query($query, JACKED_DEFAULT_LINK)){
    		if(mysql_num_rows($result) > 0){
    		    $ans = true;
    		}
        }

        return $ans;
	}

?>